//
//  Kratos_bangdingPresenter.m
//  gggg
//
//  Created by K on 2019/1/9.
//  Copyright © 2019 K. All rights reserved.
//

#import "Kratos_bangdingPresenter.h"
#import "Kratos_bangdingView.h"
#import "Kratos_inputCode.h"
#import "Kratos_bangdingInputView.h"
#import "Kratos_userInfo.h"
#import "MBProgressHUD+HUD.h"

@interface Kratos_bangdingPresenter() <Kratos_bangdingViewDelegate>
@property (weak, nonatomic) UIViewController *weakVC;

@end

@implementation Kratos_bangdingPresenter

- (void)Kratos_usageLogic
{
    if ([self.view1 isKindOfClass:[Kratos_bangdingView class]]) {
        Kratos_bangdingView *registerViwe =  (Kratos_bangdingView *)self.view1;
        registerViwe.delegate  = self;
    }
    
    
}


- (void)setViewController:(UIViewController *)ViewController
{
    _weakVC = ViewController;
}

- (void)Kratos_bangdingClick:(UIButton *)btn {
    
    __weak typeof(self) weakSelf = self;
    
    Kratos_bangdingView *registerView =  (Kratos_bangdingView *)weakSelf.view1;
    NSString *username = registerView.bangdingInputView.Kra_phoneNumber;
    NSString *code = registerView.bangdingInputView.Kra_phonecode;
    
    if (username.length == 0 || username == nil) {
        [registerView makeToast:@"请输入手机号码"];
        return;
    }
    
    if (code.length == 0 || code == nil) {
        [registerView makeToast:@"请输入验证码"];
        return;
    }
    
    NSDictionary *dic = @{@"mobile":username,@"sms":code};

    Kratos_userInfo *userinfo = [Kratos_userInfo Kratos_getCurrentUserInfo_Kratos];
    NSString *url = [NSString stringWithFormat:@"/user/user/bindapi?uid=%@&token=%@",userinfo.Kra_ID,userinfo.Kra_token];

    [self.Kra_httpClient Kratos_post:url Kratos_parameters:dic Kratos_onSuccess:^(Kratos_HttpResponse * _Nullable response) {
        
        if (response.Kra_status == Kratos_HttpResponseStatusSuccess) {
            if ([response.Kra_content[@"code"] integerValue] != 1) {
                if (response.Kra_content[@"msg"])
                {
                    [MBProgressHUD showMessage:response.Kra_content[@"msg"] toView:registerView];
                }
                return;
            }
            
            if ([response.Kra_content[@"code"] integerValue] == 1) {
              
                
                if (response.Kra_content[@"msg"])
                {
                    [MBProgressHUD showMessage:response.Kra_content[@"msg"] toView:registerView];
                }
        
                
            }
        }else
        {
            if (response.Kra_content[@"msg"])
            {
                [MBProgressHUD showMessage:response.Kra_content[@"msg"] toView:registerView];
            }
        }
        
 
    } Kratos_onFail:^(Kratos_HttpResponse * _Nullable response) {
        
    }];
    
}

- (void)Kratos_dissmissVC {
    [_weakVC dismissViewControllerAnimated:false completion:nil];
}

- (void)Kratos_getmsgCodeClick:(NSString *)phoneNumber {
    __weak typeof(self) weakSelf = self;
    Kratos_bangdingView *registerView =  (Kratos_bangdingView *)weakSelf.view1;
    
    [self.Kra_httpClient Kratos_noGeneralpost:[NSString stringWithFormat:@"/index/pub/smsrqcode?username=%@&type=4",phoneNumber] Kratos_parameters:nil onSuccess:^(Kratos_HttpResponse * _Nullable response) {
        if (response.Kra_status == Kratos_HttpResponseStatusSuccess)
        {
            if ([response.Kra_content isKindOfClass:[NSDictionary class]]) {
                NSDictionary *dict = response.Kra_content;
                [weakSelf.view1 makeToast:dict[@"msg"]];
            }
            
        }else
        {
            NSDictionary *dict = response.Kra_content;
            [weakSelf.view1 makeToast:dict[@"msg"]];
        }
        
    } onFail:^(Kratos_HttpResponse * _Nullable response) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"stopCountDown" object:nil];
        [registerView makeToast:response.Kra_content];
        
    }];
}



@end
